package com.example.treehole

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.treehole.ui.component.AppTopBar
import com.example.treehole.ui.component.BottomNavigationBar
import com.example.treehole.ui.component.CustomSnackbarHost
import com.example.treehole.ui.model.SnackbarManager
import com.example.treehole.ui.model.SquareViewModel
import com.example.treehole.ui.model.UserViewModel
import com.example.treehole.ui.page.createPost.CreatePostScreen
import com.example.treehole.ui.page.login.InitialScreen
import com.example.treehole.ui.page.myself.MyComments
import com.example.treehole.ui.page.myself.MyPosts
import com.example.treehole.ui.page.myself.MySelfViewModel
import com.example.treehole.ui.page.myself.MyselfScreen
import com.example.treehole.ui.page.postDetail.PostDetailScreen
import com.example.treehole.ui.page.postDetail.PostDetailViewModel
import com.example.treehole.ui.page.square.SquareScreen
import com.example.treehole.ui.page.treehole.PostListViewModel
import com.example.treehole.ui.page.treehole.TreeholeScreen
import com.example.treehole.ui.theme.backCyan

val LocalNavController = compositionLocalOf<NavController> { error("NavController not provided") }
val LocalSquareViewModel =
    compositionLocalOf<SquareViewModel> { error("SquareViewModel not provided") }
val LocalPostListViewModel =
    compositionLocalOf<PostListViewModel> { error("PostListViewModel not provided") }
val LocalUserViewModel = compositionLocalOf<UserViewModel> { error("UserViewModel not provided") }
val LocalPostDetailViewModel =
    compositionLocalOf<PostDetailViewModel> { error("PostDetailViewModel not provided") }
val LocalMyselfViewModel =
    compositionLocalOf<MySelfViewModel> { error("MyselfViewModel not provided") }

@Composable
fun TreeholeApp() {
    val navController = rememberNavController()

    val localSquareViewModel: SquareViewModel = viewModel()
    val localPostListViewModel: PostListViewModel = viewModel()
    val userViewModel: UserViewModel = viewModel()
    val isLoggedIn by userViewModel.isLoggedIn.collectAsState()
    val localPostDetailViewModel: PostDetailViewModel = viewModel()
    val localMySelfViewModel: MySelfViewModel = viewModel()
    var showBottomBar by remember { mutableStateOf(true) }

    LaunchedEffect(navController) {
        navController.addOnDestinationChangedListener { _, destination, _ ->
            showBottomBar =
                destination.route != "postDetail" &&
                        destination.route != "myPosts" &&
                        destination.route != "myComments" &&
                        destination.route != "createPost"
        }
    }

    CompositionLocalProvider(
        LocalNavController provides navController,
        LocalSquareViewModel provides localSquareViewModel,
        LocalPostListViewModel provides localPostListViewModel,
        LocalUserViewModel provides userViewModel,
        LocalPostDetailViewModel provides localPostDetailViewModel,
        LocalMyselfViewModel provides localMySelfViewModel
    ) {
        // 在程序启动时显示登录界面
        if (!isLoggedIn) {
            InitialScreen() // 登录成功后切换状态
        } else {
            Surface(modifier = Modifier.fillMaxSize()) {
                Scaffold(
                    // 顶部页头
                    topBar = { AppTopBar(navController) },
                    // 底部导航栏
                    bottomBar = { if (showBottomBar) BottomNavigationBar(navController) },
                    // 提示信息
                    snackbarHost = {
                        CustomSnackbarHost(SnackbarManager.treeholeSnackbarHostState)
                    }
                ) { padding ->
                    NavHost(
                        navController,
                        startDestination = "treehole",
                        modifier = Modifier
                            .padding(padding)
                            .background(backCyan)
                    ) {
                        composable("treehole") { TreeholeScreen() }
                        composable("square") { SquareScreen() }
                        composable("myself") { MyselfScreen() }
                        composable("postDetail") { PostDetailScreen() }
                        composable("myPosts") { MyPosts() }
                        composable("myComments") { MyComments() }
                        composable("createPost") { CreatePostScreen() }
                    }
                }
            }
        }
    }
}
